﻿/******************************************************************************
* File description: CustomToast.qml
* Function: 弹出来几秒就收回去的提示信息
* Current Version: 1.0.0
 *****************************************************************************/

import QtQuick 2.0
import QtGraphicalEffects 1.12
import QtQuick.Controls 2.12

Popup {
    id: root
    property int type: 0    // 0正确 1错误 2警告
    property string text: "这是一个空的消息提示"

    height: 50*dp
    width: 310*dp

    Timer{
        id: timer_toast
        interval: 2500
        repeat: false
        triggeredOnStart: false
        onTriggered: {
            up()
        }

    }

    function show(msg, type){
        down()
        root.type = type
        root.text = msg
        timer_toast.restart()
    }

    function down(){    // 从上向下滑下来
        root.visible = true
        rect.state = "downState"
    }
    function up(){      // 再滑上去
        rect.state = "upState"
        root.visible = false
    }

    background: Item{
        anchors.margins: 0
    }

    contentItem: Item{

        anchors.margins: 0

        Rectangle{
            id: rect
            height: 44*dp
            width: 300*dp
            color: "green"
            state: "upState"
            states:[
                State {
                    name: "downState"
                    PropertyChanges {
                        target: rect
                        opacity: 1
                    }
                },
                State {
                    name: "upState"
                    PropertyChanges {
                        target: rect
                        y: -55*dp
                        opacity: 0.2
                    }
                }
            ]
            transitions: [
                Transition {
                    from: "downState"
                    to: "upState"
                    NumberAnimation{
                        properties: "y"
                        duration: 200
                        easing.type: Easing.InOutQuad
                    }NumberAnimation{
                        properties: "opacity"
                        duration: 200
                        easing.type: Easing.InOutQuad
                    }
                },Transition {
                    from: "upState"
                    to: "downState"
                    NumberAnimation{
                        properties: "y"
                        duration: 100
                        easing.type: Easing.InOutQuad
                    }NumberAnimation{
                        properties: "opacity"
                        duration: 100
                        easing.type: Easing.InOutQuad
                    }
                }
            ]


            border.color: Qt.rgba(0, 0, 0, 0.01)
            border.width: 12*dp

            layer.enabled: true
            layer.effect: DropShadow{
                color: Qt.rgba(0, 0, 0, 0.1)
                samples: 32
                radius: 12*dp
                spread: 0.1
            }


            Rectangle{

                radius: 3*dp
                anchors.fill: parent
                Text {
                    text: root.text
                    anchors.verticalCenter: parent.verticalCenter
                    x: 40*dp
                    font.pixelSize: config.fontsize
                }
                CustomImage{
                    height: 16*dp
                    width: 16*dp
                    x: 15*dp
                    y: 14*dp
                    source: type===0 ? "qrc:/picture/msgSuccess.png" : (type===1 ? "qrc:/picture/msgError.png" : "qrc:/picture/msgWarning.png")
                }
            }
        }
    }

}
